package com.sgb.kjwl.viewmodel.shopping;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.view.ui.fragment.shop.ShopGroupBuyCategoryFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ShopGroupBuyCategoryModel extends BaseViewModel {
    private ShopGroupBuyCategoryFragment fragment;

    public ShopGroupBuyCategoryModel(ShopGroupBuyCategoryFragment fragment) {
        this.fragment = fragment;
    }

    public void getFirstCategory(int mCentralized) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中...", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getCMCategory(String.valueOf(mCentralized)).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    fragment.showEmpty();
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> data = response.body().getData();
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("全部");
                    data.add(0, entity);
                    fragment.showFirstCategory(data);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                fragment.showEmpty();
            }
        });
    }
}
